var userId="";
$(function(){
	$("#userName").val(loginName);
	getParams();
	queryUsersList();
	getRoleList();//获取人员角色
});

function getParams(){
	var id = $.query.get("id");
	if(id){
		var st = new String(id.replace(/\s/g, "+"), 'utf-8');
		userId = decryptCode(st.toString());
		console.log(userId);
		getUserInfo(userId);
	}
}

function getRoleList(){
	$.ajax({
		url : '/role/list',
		type : 'post',
		data : {},
		success : function(resp){
			if(resp.success){
				$("#role_id").empty();
				$("#role_id").append('<option value="-1">--请选择--</option>');
				$.each(resp.data,function(i,data){
					$("#role_id").append('<option value="' + data.id + '">' + data.name + '</option>');
				});
			}
		}
	});
}

/**
 * 用户列表
 */
function queryUsersList() {
	$("#sample-table-1").dataTable({
		ajax : {
			type : 'post',
			url : '/user/page/list',
			data : {
				
			}
		},
		autoWidth : false,
		bServerSide : true,
		bPaginate : true,
		bLengthChange : false,
		iDisplayLength : 12,
		processing : true,
		searching : false,
		info : false,
		ordering : false,
		paging : true,
		columns : [
				{
					render : function(data, type, row, meta) {
						var r = "-";
						if (null == row.id
								|| "" == row.id) {
							r = "-";
						} else {
							r = row.id;
						}
						return '<input type="checkbox" name="selecteditems" value="'
								+ r + '" />';
					},
				},
				{
					render : function(data, type, row, meta) {
						return meta.row + 1;
					},
				},
				{
					render : function(data, type, row, meta) {
						var r = "-";
						if (null == row.userName
								|| "" == row.userName) {
							r = "-";
						} else {
							r = row.userName;
						}
						return r;
					},
				},
				{
					render : function(data, type, row, meta) {
						var r = "-";
						if (null == row.realName
								|| "" == row.realName) {
							r = "-";
						} else {
							r = row.realName;
						}
						return r;
					},
				},
				{
					render : function(data, type, row, meta) {
						var r = "-";
						if (null == row.phone
								|| "" == row.phone) {
							r = "-";
						} else {
							r = row.phone;
						}
						return r;
					},
				},
				{
					render : function(data, type, row, meta) {
						var r = "-";
						if (null == row.roleName
								|| "" == row.roleName) {
							r = "-";
						} else {
							r = row.roleName;
						}
						return r;
					},
				},
				{
					render : function(data, type, row, meta) {
						var r = "-";
						if (null == row.createTime
								|| "" == row.createTime) {
							r = "-";
						} else {
							r = row.createTime;
						}
						return r;
					},
				},
				{
					render : function(data, type, row, meta) {
						return '<a href="operateUser.html?id='+encryptStr(row.id)+'" ><i class="icon-list-alt" title="详情" style="font-size:16px;"></i></a>';
					},
				}],
		language : {
			emptyTable : '暂无数据',
			processing : '查询中',
			paginate : {
				previous : '上一页',
				next : '下一页'
			}
		}
	});
}

/**
 * 根据条件查询
 */
function query() {
	var oSettings = $("#sample-table-1").dataTable().fnSettings();
	oSettings.ajax.data.userName = $.trim($("#userName").val());
	oSettings.ajax.data.phone = $.trim($("#phone").val());
	oSettings.ajax.data.roleId = $.trim($("#role_id").val());
	//oSettings._iDisplayLength = 50;动态设置一页显示码数
	$("#sample-table-1").dataTable().fnDraw(oSettings);
}

/**
 * 调整一页显示行数
 * @returns
 */
function chageRow(row) {
	var oSettings = $("#sample-table-1").dataTable().fnSettings();
	oSettings._iDisplayLength = row;//动态设置一页显示码数
	$("#sample-table-1").dataTable().fnDraw(oSettings);
}

/**
 * 跳转指定页码
 * @returns
 */
function chagePage(page){
	var oTabel = $("#sample-table-1").dataTable();
	oTabel.fnPageChange(page);
}

function deleteUser(){
	var deleteIds = new Array();
	$.each($("input:checkbox:checked"),function(i,data){
		deleteIds.push($(this).attr("value")) ;
	});
	
	if(deleteIds.length == 0){
		art.dialog({
			icon : 'error',
			time : 2,
			content : '请选择要删除的用户！'
		});
		return false;
	}
	/*var datas = encryptCode({
		ids : deleteIds.join(',')
	});*/
	art.dialog({
			icon : 'warning',
			content : "确定要删除该记录吗",
			cancelVal:'关闭',
			cancel:true,
			okVal: '确定',
			ok:function(){
				$.ajax({
					url : '/user/delete',
					type : 'post',
					data : {
						ids : deleteIds.join(',')
					},
					success : function(data){
						if(data.success){
							art.dialog({
								icon : 'succeed',
								time : 2,
								content : data.msg
							});
							setTimeout(function(){
								$('#sample-table-1').DataTable().ajax.reload(); 
							},2000);
						}else if(data.code == 1006){
							window.location.href="login.html";
						}else{
							art.dialog({
								icon : 'error',
								time : 2,
								content : data.msg
							});
						}
					}
				});
			}
		});
}

/**
 * 保存用户信息
 * @returns
 */
function operateUser(){
	var userName = $("#userName").val();
	if(loginName == ""){
		art.dialog({
			icon : 'error',
			time : 2,
			content : '请输入登录名称！'
		});
		$("#userName").focus();
		return false;
	}
	//昵称
	var realName = $("#realName").val();
	//角色
	var roleId = $("#role_id").val();
	if(roleId == '-1'){
		art.dialog({
			icon : 'error',
			time : 2,
			content : '请选择用户角色！'
		});
		return false;
	}
	var phone = $("#phone").val();
	var isPhone =/^(13[0-9]|14[5-9]|15[012356789]|166|17[0-8]|18[0-9]|19[8-9])[0-9]{8}$/;  
    var isMob= /^0\d{2,3}\d{7,9}(-\d{1,6})?$/;  
    
    if(phone == ''){
    	art.dialog({
			icon : 'error',
			time : 2,
			content : '手机号不能为空'
		});
    	$("#phone").focus();
		return false;
    }
    if(!isPhone.test(phone) && !isMob.test(phone)){
    	art.dialog({
			icon : 'error',
			time : 2,
			content : '手机号格式有误,请核对后进行输入'
		});
		return false;
    }
	var datas = {
		id : userId,
		userName : userName,	
		realName : realName,
		phone : phone,
		roleId : roleId
	};
	//用户名和手机号唯一校验
	$.ajax({
		url : '/user/checkUserInfo',
		data : {
			userName : userName,
			phone : phone
		},
		type : 'post',
		success : function(data){
			if(data.success){
				//新增修改用户
				$.ajax({
					url : '/user/operate',
					data : datas,
					type : 'post',
					success : function(data){
						if(data.success){
							art.dialog({
								icon : 'succeed',
								time : 2,
								content : data.msg
							});
							setTimeout(function(){
								window.location.href = 'users.html';
							},2000);
						}else if(data.code == 1006){
							window.location.href="login.html";
						}else{
							art.dialog({
								icon : 'error',
								time : 2,
								content : data.msg
							});
						}
					}
				});
			}else if(data.code == 1006){
				window.location.href="login.html";
			}else{
				art.dialog({
					icon : 'error',
					time : 2,
					content : data.msg
				});
			}
		}
	});
}

function getUserInfo(userId){
	//人员id
	$.ajax({
		url : '/user/info',
		type : 'post',
		data : {
			id : userId
		},
		success : function(resp){
			if(resp.success){
				var user = resp.data.user;
				var roleList = resp.data.roleList;
				$("#userName").val(user.userName);
				$("#userName").attr("readonly","readonly");
				$("#realName").val(user.realName);
				//$("input[name='sex_dl'][value='"+data.sex+"']").attr('checked',true);
				$("#phone").val(user.phone);
				$("#role_id").empty();
				$("#role_id").append('<option value="-1">--请选择--</option>');
				$.each(roleList,function(i,data){
					if(data.id == user.roleId){
						$("#role_id").append('<option value="' + data.id + '" selected="selected">' + data.name + '</option>');
					} else {
						$("#role_id").append('<option value="' + data.id + '">' + data.name + '</option>');
					}
				});
			}else if(data.code == 1006){
				window.location.href="login.html";
			}
		}
	});
}

function updateUser(){
	var userName = $("#userName").val();
	if(userName == ""){
		art.dialog({
			icon : 'error',
			time : 2,
			content : '请输入登录名称！'
		});
		$("#userName").focus();
		return false;
	}
	var new_pwd = $("#new_pwd").val();
	if(new_pwd == ''){
		art.dialog({
			icon : 'error',
			time : 2,
			content : '请请输入新密码！'
		});
		$("#new_pwd").focus();
		return false;
	}
	var confirm_pwd = $("#confirm_pwd").val();
	if(confirm_pwd == ''){
		art.dialog({
			icon : 'error',
			time : 2,
			content : '请输入确认新密码！'
		});
		$("#confirm_pwd").focus();
		return false;
	}
	console.log(new_pwd);
	console.log(confirm_pwd);
	if(new_pwd != confirm_pwd){
		art.dialog({
			icon : 'error',
			time : 2,
			content : '请确认密码是否相同！'
		});
		$("#confirm_pwd").focus();
		return false;
	}
	var datas = {
		userName : userName,	
		userPwd : hex_md5(new_pwd)
	};
	//用户名和手机号唯一校验
	$.ajax({
		headers : {
			token : token
		},
		url : '/user/update',
		data : datas,
		type : 'post',
		success : function(data){
			if(data.success){
				art.dialog({
					icon : 'succeed',
					time : 2,
					content : data.msg
				});
				window.location.href = 'operatePwd.html';
			}else if(data.code == 1006){
				window.location.href="login.html";
			}else{
				art.dialog({
					icon : 'error',
					time : 2,
					content : data.msg
				});
			}
		}
	});
}
